//
//  ViewController.m
//  WKWebviewDemo
//
//  Created by fangyuan ding on 2020/10/27.
//  Copyright © 2020 fangyuan ding. All rights reserved.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>
@interface ViewController ()<WKUIDelegate,WKNavigationDelegate>

@property (nonatomic, strong)WKWebView *webView ;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
//    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL
//    URLWithString:@"http://www.baidu.com"]]];
//    [self.view addSubview:webView];
    
    WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];
    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds
    configuration:configuration];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL
    URLWithString:@"http://www.baidu.com"]]];
    [self.view addSubview:self.webView];
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
}
#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation
*)navigation{
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation
*)navigation{
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation
*)navigation{
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation
*)navigation{
}
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView
didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
}
// 在收到响应后，决定是否跳转
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse
decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
NSLog(@"%@",navigationResponse.response.URL.absoluteString);

//允许跳转
decisionHandler(WKNavigationResponsePolicyAllow);
//不允许跳转
//decisionHandler(WKNavigationResponsePolicyCancel);
}
// 在发送请求之前，决定是否跳转
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
//    WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
//    NSURL *url = navigationAction.request.URL;
//    if(policy == WKNavigationActionPolicyAllow && [url.host containsString:@"showjoy.com"]){
//        NSDictionary *dic = navigationAction.request.allHTTPHeaderFields;
//        NSString *cookie = [dic objectForKey:@"Cookie"];
//        if(cookie == nil){
//            NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:navigationAction.request.URL];
//            request.allHTTPHeaderFields = dic;
//            [request setValue:@"替换custom cookie" forHTTPHeaderField:@"Cookie"];
//            [webView loadRequest:request];
//            policy = WKNavigationActionPolicyCancel;
//        }
//    }
//    if(decisionHandler){
//        decisionHandler(policy);
//    }
    //WebKit框架对跨域进行了安全性检查限制，不允许跨域，比如从一个 HTTP 页对 HTTPS 发起请求是无效的
    WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
       NSURL *url = navigationAction.request.URL;
       if (WKNavigationTypeLinkActivated == navigationAction.navigationType && [url.scheme isEqualToString:@"https"]) {
           [[UIApplication sharedApplication] openURL:url];
           policy = WKNavigationActionPolicyCancel;
       }
       if (decisionHandler) {
           decisionHandler(policy);
       }
NSLog(@"%@",navigationAction.request.URL.absoluteString);
//允许跳转
//decisionHandler(WKNavigationActionPolicyAllow);
//不允许跳转
//decisionHandler(WKNavigationActionPolicyCancel);
}
#pragma mark - WKUIDelegate
// 创建一个新的WebView
-(WKWebView*)webView:(WKWebView*)webView
createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration
forNavigationAction:(WKNavigationAction*)navigationAction
windowFeatures:(WKWindowFeatures *)windowFeatures{
return [[WKWebView alloc]init];
}
// 输入框
-(void)webView:(WKWebView*)webView
runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt defaultText:(nullable
NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void
(^)(NSString * __nullable result))completionHandler{
completionHandler(@"http");
}
// 确认框
-(void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString
*)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL
result))completionHandler{
completionHandler(YES);
}
// 警告框
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString
*)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void
(^)(void))completionHandler{
NSLog(@"%@",message);
completionHandler();
}

@end
